﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Mvc;
using MovieAPI.Models;
namespace MovieAPI.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*"), RequireHttpsAttribute]
    public class HallController : ApiController
    {
        private filmEntities db = new filmEntities();
        public IHttpActionResult GetHallByHallID(int id)
        {

            return Ok(Hall(id));
        }
        public static dynamic Hall(int id)
        {
            using (filmEntities db = new filmEntities())
            {
                var hall = db.Hall.Where(H => H.HallID == id).Select(H => H.HallSeat).FirstOrDefault();
                string[] HallRow = hall.Split('=');
                string[] StringHallCol = HallRow[0].Split(','); //string[,]是等长数组，列维度一样，只要取任意一行的列维度即可确定整个二维数组的列维度
                int[] IntHallCol = Array.ConvertAll<string, int>(StringHallCol, delegate (string s) { return int.Parse(s); });
                int x = HallRow.Length;
                int y = StringHallCol.Length;
                int[,] twoArray = new int[x, y];
                for (int i = 0; i < x; i++)
                {
                    StringHallCol = HallRow[i].Split(',');
                    IntHallCol = Array.ConvertAll<string, int>(StringHallCol, delegate (string s) { return int.Parse(s); });
                    for (int j = 0; j < IntHallCol.Length; j++)
                    {
                        twoArray[i, j] = IntHallCol[j];
                    }
                }
                dynamic HallArray = new
                {
                    twoArray,
                    x,
                    y
                };
                return HallArray;
            }

        }
        public IHttpActionResult GetHallInfo()
        {
            using (filmEntities db = new filmEntities())
            {
                List<int> HallID = db.Hall.Select(H => H.HallID).ToList();
                List<dynamic> HallSeat = new List<dynamic>();
                foreach (var item in HallID)
                {
                    HallSeat.Add(Hall(item));
                }
            var hall = db.Hall.ToList();
                List<dynamic> HallInfo = new List<dynamic>();
                for (int i = 0; i < HallSeat.Count; i++)
                {
                    HallInfo.Add(new{ hall=hall[i], hallSeat=HallSeat[i]});
                }
                return Ok(HallInfo);
            }

        }

        public IHttpActionResult GetHall() {
            using (filmEntities db = new filmEntities())
            {
                var hall = db.Hall.ToList();
                return Ok(hall);
            }
        }
        public IHttpActionResult AddHall(dynamic hall)
        {
            using (filmEntities db=new filmEntities())
            {
                Hall HallInfo = new Hall();
                HallInfo.HallName = hall.Hall.hall.HallName;
                HallInfo.HallSeat = hall.Seat;
                HallInfo.HallType = hall.Hall.hall.HallType;
                db.Hall.Add(HallInfo);
                db.SaveChanges();
                return CreatedAtRoute("DefaultApi", new { id = HallInfo.HallID }, HallInfo);
            } 
        }
        public IHttpActionResult EditHall(dynamic hall)
        {
            using (filmEntities db = new filmEntities())
            {
                int ID = hall.Hall.hall.HallID;
                Hall HallInfo = db.Hall.Where(H=>H.HallID==ID).FirstOrDefault();
                HallInfo.HallName = hall.Hall.hall.HallName;
                HallInfo.HallSeat = hall.Seat;
                HallInfo.HallType = hall.Hall.hall.HallType;

                db.SaveChanges();
                return CreatedAtRoute("DefaultApi", new { id = HallInfo.HallID }, HallInfo);
            }
        }
    }
}
